package com.tuanbiao.presenter.account;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.hzjq.entity.UserEntity;
import com.tuanbiao.AppConfig;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.constants.UserConstants;
import com.tuanbiao.controllers.AccountController;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.keyevent.KeyEventUtils;
import com.tuanbiao.utils.SPHelper;

/**
 * @author abiao
 * @since 2022/3/31 9:20
 */
public class LoginPresenter extends BasePresenter<AccountController.Model, AccountController.LoginView> implements AccountController.LoginPresenter {

    public LoginPresenter(AccountController.Model model, AccountController.LoginView rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void init() {
        mRootView.showAppVersion("软件版本号：v_" + AppConfig.getVersionName());
        mRootView.showUserName(SPHelper.userName());
        if (AppConfig.showTestData()) {
            //  mRootView.showLoginInfo("522730199007058370 ", "123456");
            mRootView.showLoginInfo("110101199003070652", "123456");
        }
    }

    @Override
    public void login(String idCard, String password) {
//        BlastKool.Companion.loader().setMacImei("b4:48:3d:5a:5c:13", "864881042855483");
        if (TextUtils.isEmpty(idCard)) {
            mRootView.showMessage(R.string.login_id_card_hint);
            return;
        }
        if (TextUtils.isEmpty(password)) {
            mRootView.showMessage(R.string.login_password_hint);
            return;
        }
        mModel.login(idCard, password, new OnBaseResultCallback<UserEntity>() {
            @Override
            public void onSuccess(UserEntity userEntity) {
                SPHelper.setUserName(idCard);
                SPHelper.setLoginValid(true);
                mRootView.startMain();
            }

            @Override
            public void onFailed(String msg) {
                mRootView.showMessage(msg);
            }
        });
    }

    @Override
    public void checkPassword(String content) {
        if (TextUtils.equals(content, KeyEventUtils.getInstance().getLoginF2f3Password())) {
            mRootView.startDeviceSetting(UserConstants.Type.NORMAL);
        } else if (TextUtils.equals(content, KeyEventUtils.getInstance().getLoginF2f3Password2())){
            mRootView.startDeviceSetting(UserConstants.Type.TEST);
        }else {
            mRootView.showMessage("密码错误");
        }
    }

    @Override
    public void checkMacSettingPassword(String content) {
        if (TextUtils.equals(content, KeyEventUtils.getInstance().getLoginF1f4Password())) {
            mRootView.startMacSetting();
        } else {
            mRootView.showMessage("密码错误");
        }
    }
}
